import Vue from 'vue'
import App from './App.vue'
import router from '@/router'
import store from '@/store'
import qs from "qs";
import { redirectOCAuth, getWechatUser } from "@/api/index";
import { Toast, Dialog, Lazyload, Icon } from "vant";
import 'vant/lib/icon/local.css';
import 'vant/lib/index.css';
Vue.use(Toast)
	.use(Icon)
	.use(Dialog)
	.use(Lazyload);

Vue.config.productionTip = false

// const VConsole = require("vconsole");
// window.vConsole = new VConsole();

// sessionStorage.setItem("token", "900ea1336ed48cb3fd41da398a97938f"); //dae4e325d86c67735695736c93c43f27
const token = sessionStorage.getItem("token");
if (!token) {
	if (window.location.href.indexOf("code") == 0 || window.location.href.indexOf("code") <= 0) {
		redirectOCAuth()
	} else {
		function getQueryString(name) {
			let reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
			let r = window.location.search.substr(1).match(reg);
			if (r != null) {
				return unescape(r[2]);
			} else {
				return null;
			}
		}
		let code = getQueryString("code");
		// const res = await getWechatUser({code:code});
		getWechatUser({ code: code }).then(res => {
			window.location.search = "";
			// alert(window.location.search)
			sessionStorage.setItem("token", res.token)
			new Vue({
				router,
				store,
				render: h => h(App)
			}).$mount("#app");
		})
	}
} else {
	new Vue({
		router,
		store,
		render: h => h(App)
	}).$mount("#app");
}


